package com.tangyee.home_exercise_step.activities;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangyee.R;
import com.tangyee.home_exercise_step.bean.HistoryData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StepHistory extends AppCompatActivity {

    private HistoryData historyData;

    private ViewPager viewPager;
    private List<View> viewList;
    private View stepHistoryItem;
    private List<HistoryData> historyDataList;
    public TextView historyStepNumber;
    public TextView distanceText;
    public TextView saveGasoline;
    public TextView heatText;
    public TextView burnFat;

    private TextView historyText;

    private ImageView historyBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_history);

        //得到当前界面的装饰视图
        if(Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            //让应用主题内容占用系统状态栏的空间,注意:下面两个参数必须一起使用 stable 牢固的
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //设置状态栏颜色为透明
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        historyText = findViewById(R.id.history_text);
        historyData = new HistoryData();
        showHistory();

        historyBack = findViewById(R.id.history_back);
        historyBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void showHistory() {
        viewPager = findViewById(R.id.viewPager);
        LayoutInflater layoutInflater = getLayoutInflater();
        historyDataList = historyData.getData();

        viewList = new ArrayList<>();
        for (int i = 0; i < historyDataList.size(); i++) {
            stepHistoryItem = layoutInflater.inflate(R.layout.activity_step_history_item, null);
            historyStepNumber = stepHistoryItem.findViewById(R.id.history_step_number);
            distanceText = stepHistoryItem.findViewById(R.id.distance_text);
            saveGasoline = stepHistoryItem.findViewById(R.id.save_gasoline);
            heatText = stepHistoryItem.findViewById(R.id.heat_text);
            burnFat = stepHistoryItem.findViewById(R.id.burn_fat);
            historyStepNumber.setText(historyDataList.get(i).getHistoryStepNumber());
            distanceText.setText(historyDataList.get(i).getDistanceText());
            saveGasoline.setText(historyDataList.get(i).getSaveGasoline());
            heatText.setText(historyDataList.get(i).getHeatText());
            burnFat.setText(historyDataList.get(i).getBurnFat());
            viewList.add(stepHistoryItem);
        }

        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(viewList.get(position));
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);

            }



        };

        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(historyDataList.size() - 1);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                historyData = historyDataList.get(i);
                if (getTodayDate().equals(historyData.getDate())) {
                    historyText.setText("今日步数");
                } else {
                    historyText.setText(historyData.getDate());
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    //获得今日的日期
    private String getTodayDate() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

}
